package com.qf.commons.event.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 构建者模式 - 创建复杂对象
 *
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@Data
public class QphoneMessage implements Serializable {
    //消息的id
    private String msgId;
    //事件类型 - 开发者自定义
    private String eventType;
    //本身消息类型 0 - 迅捷消息 1 - 可靠性消息
    private int msgType;
    //消息延迟的毫秒值  -1 非延迟消息
    private long delayMs;
    //消息的发送时间
    private Date sendTime;
    //消息的接收时间
    private Date recivTime;
    //事件内容
    private Object eventContent;
    //额外的属性
    private Map<String, Object> attr;

    /**
     * 私有构造方法
     */
    private QphoneMessage(){}

    /**
     * 提供一个当前对象的构建器
     */
    public static class Builder{
        //消息的id
        private String msgId;
        //事件类型 - 开发者自定义
        private String eventType;
        //本身消息类型 0 - 迅捷消息 1 - 可靠性消息
        private int msgType;
        //消息延迟的毫秒值  -1 非延迟消息
        private long delayMs;
        //消息的发送时间
        private Date sendTime;
        //消息的接收时间
        private Date recivTime;
        //事件内容
        private Object eventContent;
        //额外的属性
        private Map<String, Object> attr;

        /**
         * 构造方法 - 必须的属性 通过构造方法设置
         */
        public Builder(String eventType, Object eventContent){
            this.eventType = eventType;
            this.eventContent = eventContent;
            this.attr = new HashMap<>();
        }

        public Builder setMsgId(String msgId) {
            this.msgId = msgId;
            return this;
        }

        public Builder setMsgType(int msgType) {
            this.msgType = msgType;
            return this;
        }

        public Builder setDelayMs(long delayMs) {
            this.delayMs = delayMs;
            return this;
        }

        public Builder setSendTime(Date sendTime) {
            this.sendTime = sendTime;
            return this;
        }

        public Builder setRecivTime(Date recivTime) {
            this.recivTime = recivTime;
            return this;
        }

        public Builder addAttr(String key, String value) {
            this.attr.put(key, value);
            return this;
        }

        /**
         * 构建方法
         */
        public QphoneMessage build(){
            QphoneMessage qphoneMessage = new QphoneMessage();
            qphoneMessage.msgId = msgId;
            qphoneMessage.eventType = eventType;
            qphoneMessage.msgType = msgType;
            qphoneMessage.delayMs = delayMs;
            qphoneMessage.sendTime = sendTime;
            qphoneMessage.recivTime = recivTime;
            qphoneMessage.eventContent = eventContent;
            qphoneMessage.attr = attr;
            return qphoneMessage;
        }
    }

}
